﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectSpreadActivity : WebActivity
    {
        public void Subject(WebRequest request, WebResponse response, String type)
        {
            var sev = request.SendValues ?? new UMC.Web.WebMeta();
            ulong? project_id = null;
            ulong? subid = null;
            if (sev.ContainsKey("Id"))
            {
                var cid = UMC.Data.Utility.UInt64Parse(sev["Id"], 0);
                if (cid > 0)
                {
                    var sub = DataFactory.Instance().Subject(cid);
                    if (sub != null && sub.project_id.HasValue)
                    {
                        project_id = sub.project_id.Value;
                        subid = sub.Id.Value;
                    }

                }

            }
            var sp = new Subject() { project_id = project_id };
            if (String.Equals(type, "View"))
            {
                sp.Look = 0;
            }
            else
            {
                sp.Reply = 0;

            }
            var data = new System.Data.DataTable();
            data.Columns.Add("id");
            data.Columns.Add("title");
            data.Columns.Add("desc");
            data.Columns.Add("src");
            data.Columns.Add("reply");
            data.Columns.Add("look");
            data.Columns.Add("last");

            var webr = UMC.Web.WebResource.Instance();
            UMC.Data.Utility.Each(DataFactory.Instance().Spread(sp), (Subject dr) =>
            {
                if (dr.Id == subid)
                {
                    return;
                }
                data.Rows.Add(dr.Id, dr.Title, dr.Description, webr.ImageResolve(request.Url,dr.Id.Value, 1, "-3"), dr.Reply, dr.Look, UMC.Data.Utility.GetDate(dr.LastDate));
            });
            response.Redirect(data);
        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var type = this.AsyncDialog("Type", "View");
            switch (type)
            {
                case "View":
                case "Reply":
                    this.Subject(request, response, type);
                    break;
                case "Favs":
                    {
                        var category = Utility.UInt64Parse(this.AsyncDialog("Project", "Favs"), 0);


                        var TimeSpan = Utility.TimeSpan();

                        var data = new System.Data.DataTable();
                        data.Columns.Add("id");
                        data.Columns.Add("title");
                        data.Columns.Add("desc");
                        data.Columns.Add("src");
                        data.Columns.Add("path");
                        data.Columns.Add("ppath");
                        data.Columns.Add("project");
                        data.Columns.Add("last");
                        data.Columns.Add("poster");
                        data.Columns.Add("uid");

                        var subs = new List<Subject>();

                        var pris = new List<ulong>();
                        var itemids = new List<ulong>();
                        var uids = new List<Guid>();

                        var webr = UMC.Web.WebResource.Instance();
                        UMC.Data.Utility.Each(DataFactory.Instance().Spread(new Subject { project_id = category, Favs = 0 }), dr =>
                          {
                              subs.Add(dr);
                              pris.Add(dr.project_id.Value);
                              itemids.Add(dr.project_item_id.Value);
                              uids.Add(dr.user_id.Value);
                          });
                        var cates = new List<Project>();
                        var pitems = new List<ProjectItem>();
                        // var users = new List<Data.Entities.User>();


                        if (itemids.Count > 0)
                        {
                            pitems.AddRange(DataFactory.Instance().ProjectItem(itemids.ToArray()));
                        }
                        if (pris.Count > 0)
                        {
                            cates.AddRange(DataFactory.Instance().Projects(pris.ToArray()));

                        }
                        foreach (var sub in subs)
                        {
                            // var u = users.Find(d => d.Id == sub.user_id) ?? new Data.Entities.User { Alias = sub.Poster };
                            var cate = cates.Find(g => g.Id == sub.project_id);
                            var pitem = pitems.Find(g => g.Id == sub.project_item_id);
                            if (cate != null && pitem != null)
                            {
                                data.Rows.Add(sub.Id, sub.Title, sub.Description, webr.ImageResolve(request.Url,sub.Id.Value, "1", "0"), String.Format("{0}/{1}/{2}", cate.Code, pitem.Code, sub.Code), cate.Code, cate.Caption, Utility.GetDate(sub.ReleaseTime), sub.Poster, sub.user_id);

                            }
                        }


                        response.Redirect(new WebMeta().Put("data", data));



                    }

                    break;
                case "NewProject":
                case "Project":
                    {
                        var webr = UMC.Web.WebResource.Instance();
                        var ids = new List<Guid>();
                        var subMebs = new List<ProjectMember>();


                        var projects = new List<Project>();
                        var pids = new List<ulong>();
                        var order = type == "NewProject" ? new Project { CreationTime = DateTime.MinValue } : new Project { Sequence = 0 };



                        if (projects.Count > 0)
                        {


                            var data = new System.Data.DataTable();
                            data.Columns.Add("id");
                            data.Columns.Add("title");
                            data.Columns.Add("desc");
                            data.Columns.Add("src");
                            data.Columns.Add("path");
                            data.Columns.Add("subs");
                            data.Columns.Add("members");

                            foreach (var p in projects)
                            {
                                data.Rows.Add(p.Id, p.Caption, p.Description, webr.ImageResolve(request.Url,p.Id.Value, "1", "4"), p.Code, p.SubjectTotal, (p.MemberTotal ?? 0) + 1);
                            }
                            response.Redirect(data);
                        }
                    }
                    break;
                case "Cognate":
                    {

                        var projectId = Utility.UInt64Parse(this.AsyncDialog("Project", "Favs"), 0);

                        var project = DataFactory.Instance().Project(projectId);


                        var webr = UMC.Web.WebResource.Instance();
                        var ids = new List<ulong>();
                        var subMebs = new List<ProjectMember>();


                        var projects = new List<Project>();
                        projects.AddRange(DataFactory.Instance().Projects(project.user_id.Value));


                        if (projects.Count > 0)
                        {


                            var data = new System.Data.DataTable();
                            data.Columns.Add("id");
                            data.Columns.Add("title");
                            data.Columns.Add("desc");
                            data.Columns.Add("src");
                            data.Columns.Add("path");
                            data.Columns.Add("subs");
                            data.Columns.Add("members");

                            foreach (var p in projects)
                            {
                                data.Rows.Add(p.Id, p.Caption, p.Description, webr.ImageResolve(request.Url,p.Id.Value, "1", "4"), p.Code, p.SubjectTotal, p.MemberTotal + 1);
                            }
                            response.Redirect(data);

                        }
                    }
                    break;

                case "CognateFavs":
                    {
                        var category = Utility.UInt64Parse(this.AsyncDialog("Project", "Favs"), 0);


                        var data = new System.Data.DataTable();
                        data.Columns.Add("id");
                        data.Columns.Add("title");
                        data.Columns.Add("desc");
                        data.Columns.Add("src");
                        data.Columns.Add("path");
                        data.Columns.Add("ppath");
                        data.Columns.Add("project");
                        data.Columns.Add("last");
                        data.Columns.Add("poster");
                        data.Columns.Add("uid");
                        var subs = new List<Subject>();

                        var pris = new List<ulong>();
                        var itemids = new List<ulong>();
                        var uids = new List<Guid>();

                        var webr = UMC.Web.WebResource.Instance();
                        UMC.Data.Utility.Each(DataFactory.Instance().Spread(new Subject { project_id = category, Favs = 0 }), dr =>
                         {

                             subs.Add(dr);
                             pris.Add(dr.project_id.Value);
                             itemids.Add(dr.project_item_id.Value);
                             uids.Add(dr.user_id.Value);
                         });
                        var cates = new List<Project>();
                        var pitems = new List<ProjectItem>();
                        // var users = new List<Data.Entities.User>();


                        if (itemids.Count > 0)
                        {

                            pitems.AddRange(DataFactory.Instance().ProjectItem(itemids.ToArray()));
                        }
                        if (pris.Count > 0)
                        {

                            cates.AddRange(DataFactory.Instance().Projects(pris.ToArray()));
                        }
                        foreach (var sub in subs)
                        {
                            // var u = users.Find(d => d.Id == sub.user_id) ?? new Data.Entities.User { Alias = sub.Poster };
                            var cate = cates.Find(g => g.Id == sub.project_id);
                            var pitem = pitems.Find(g => g.Id == sub.project_item_id);
                            if (cate != null && pitem != null)
                            {
                                data.Rows.Add(sub.Id, sub.Title, sub.Description, webr.ImageResolve(request.Url,sub.Id.Value, "1", "0"), String.Format("{0}/{1}/{2}", cate.Code, pitem.Code, sub.Code), cate.Code, cate.Caption, Utility.GetDate(sub.ReleaseTime), sub.Poster, sub.user_id);

                            }
                        }
                        response.Redirect(data);





                    }
                    break;
                default:
                    break;
            }
        }

    }
}